<script setup>
import { computed, ref, watch } from 'vue'
import { fetchNowWeatherData, fetchAirData } from '../api/weatherAPI'
import { useCityStore } from '../stores/cityStore'
import 'qweather-icons/font/qweather-icons.css' //引入和风天气图标
import dayjs from 'dayjs' //引入dayjs日期库
import { fetchIndicesData } from '../api/weatherAPI'
const cityStore = useCityStore()
//声名响应式数据
const nowData = ref({})
const updateTime = ref('') //获取更新时间
const airData = ref({})
const indicesData = ref({})
watch(
  //监听数据源
  () => cityStore.location,
  //回调函数
  () => {
    fetchNowWeather(cityStore.location.id)
  },
  //即时回调
  { immediate: true }
)

async function fetchNowWeather(locationID) {
  const res = await fetchNowWeatherData(locationID)
  // console.log(res)
  nowData.value = res.now
  updateTime.value = res.updateTime
  updateTime.value = dayjs(res.updateTime).format('YYYY-MM-DD HH:mm') //把获取到的updateTime保存
  const resAir = await fetchAirData(locationID)
  // console.log(resAir)
  airData.value = resAir.now
  const resIndices = await fetchIndicesData(locationID, 5)
  // console.log(resIndices)
  indicesData.value = resIndices.daily[0]
}
const weatherDescription = computed(() => {
  //天气描述
  return `今天${nowData.value.text}，现在${nowData.value.temp}°，空气${airText.value}`
})
const airText = computed(() => {
  const aqi = airData.value.aqi || 0
  if (aqi <= 100) return '优'
  if (aqi <= 150) return '良'
  if (aqi <= 200) return '中度污染'
  if (aqi <= 300) return '轻度污染'
  if (aqi <= 500) return '重度污染'
  return '爆表'
})
</script>
<template>
  <div class="now">
    <div class="top">
      <div class="header">
        <span class="city">{{ cityStore.location.name }}</span>
        <span class="time">{{ updateTime }}</span>
      </div>
      <div class="air">
        <span>AQI {{ airText }}</span>
      </div>
    </div>

    <div class="mid">
      <div class="weather">
        <i :class="`qi-${nowData.icon}`" style="font-size: 4rem"></i>
        <div class="temp-group">
          <p class="temperature">{{ nowData.temp }}°</p>
          <p class="weather-text">{{ nowData.text }}</p>
        </div>
      </div>
      <!-- 新增天气描述文字 -->
      <div class="text">{{ weatherDescription }}</div>
      <div class="text">{{ nowData.description }}</div>
    </div>

    <!-- 修改后的底部布局 -->
    <div class="bottom">
      <div class="item">
        <p class="value">{{ nowData.windScale }}级</p>
        <p class="label">{{ nowData.windDir }}</p>
      </div>
      <div class="item">
        <p class="value">{{ nowData.humidity }}%</p>
        <p class="label">湿度</p>
      </div>
      <div class="item">
        <p class="value">{{ indicesData.category }}</p>
        <p class="label">紫外线</p>
      </div>
      <div class="item">
        <p class="value">{{ nowData.feelsLike }}°</p>
        <p class="label">体感温度</p>
      </div>
      <div class="item">
        <p class="value">{{ nowData.vis }}km</p>
        <p class="label">能见度</p>
      </div>
      <div class="item">
        <p class="value">{{ nowData.precip }}mm</p>
        <p class="label">降水量</p>
      </div>
      <div class="item">
        <p class="value">{{ nowData.pressure }}hPa</p>
        <p class="label">大气压</p>
      </div>
    </div>
  </div>
</template>

<style scoped>
.now {
  /* 保持原有容器样式 */
  width: 100%;
  background: #555f7a;
  border-radius: 16px;
  padding: 1rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 修改顶部布局 */
.top {
  display: flex;
  flex-direction: column; /* 改为垂直布局 */
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.air > span {
  text-align: center;
  width: 6rem;
  height: 2rem;
  line-height: 2rem;
  background-color: #a9a538;
  float: right;
  border-radius: 1rem;
  color: white;
}

/* 中部样式保持不变 */
.mid {
  text-align: center;
  margin: 1rem 0;
}
/*天气描述*/
.mid .text {
  font-size: 0.9rem;
  color: white;
  margin-top: 0.5rem;
  line-height: 1.4;
  text-align: center;
  padding: 0 1rem; /* 增加两侧内边距避免贴边 */
}
.weather {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  margin: 1rem 0;
}

.temp-group {
  text-align: left;
}

.temperature {
  font-size: 2.5rem;
  line-height: 1;
  margin: 0;
}

/* 修改底部为六列布局 */
.bottom {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.5rem;
  margin-top: 1rem;
}

.item {
  background: rgba(255, 255, 255, 0.9);
  padding: 0.6rem;
  border-radius: 8px;
  text-align: center;
  min-width: 60px; /* 防止内容挤压 */
}

.value {
  font-size: 1rem; /* 适当缩小字号 */
  margin: 0;
  color: #333;
}

.label {
  font-size: 0.7rem; /* 缩小标签字号 */
  color: #666;
  margin: 0.2rem 0 0;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .bottom {
    grid-template-columns: repeat(3, 1fr); /* 小屏幕显示3列 */
    gap: 0.5rem;
  }

  .item {
    padding: 0.4rem;
  }
}
</style>